// common/components/filterUserSelectPopup.js

import P from 'wl-pinyin'
const app = getApp()


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    data: {
      type: Array,
      value: []
    },
    multiple: {
      type: Boolean,
      value: true
    }
    // value: {
    //   type: String,
    //   value: ''
    // }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    indexListData: [],
    keywords: '',
    curIndex: 0,
    // 索引项偏移值
    indexTop: 0,

  },
  lifetimes: {


    attached() {},
    ready() {
      console.log('ready');

      this.initData()
      // this.initStyle()

    },
  },
  pageLifetimes: {
    show() {
      console.log('show');

    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChangeIndexList(event) {
      this.setData({
        indexListData: event.detail.value
      })
    },
    onConfirm() {
      this.triggerEvent('confirm', {
        value: this.data.indexListData
      })
    },
    onCancel() {
      this.triggerEvent('cancel')
    },

    initData() {
      let person = {}
      let arr = []
      console.log(this.data.data, 'data');
      if (this.data.data.length) {
        arr = this.data.data
      } else {
        arr = app.globalData.userList
      }
      arr.forEach(item => {
        let first = P.getFirstLetter(item.nickName).slice(0, 1)
        if (person[first]) {
          person[first].push(item)
        } else {
          person[first] = [item]
        }
      })
      const list = []
      for (let i in person) {
        list.push({
          alpha: i,
          subItems: person[i]
        })
      }

      list.sort((a, b) => {
        if (a.alpha < b.alpha) {
          return -1; // a 在 b 前面
        } else if (a.alpha > b.alpha) {
          return 1; // a 在 b 后面
        } else {
          return 0; // 顺序无变化
        }
      })
      this.setData({
        list: list,
      })
    },


  }
})